
using QdbUnit;
using Qdb;
using Qdb.Ext;
using Qdb.Tests.Common.Api;
using Qdb.Tests.Common.Assorted;

namespace Qdb.Tests.Common.Assorted
{
	public class PreventMultipleOpenTestCase : QdbTestWithTempFile
	{
		public static void Main(string[] args)
		{
			new ConsoleTestRunner(typeof(PreventMultipleOpenTestCase)).Run();
		}

		public virtual void TestOpenTwice()
		{
			IObjectContainer oc = OpenFile();
			Assert.Expect(typeof(DatabaseFileLockedException), new _ICodeBlock_21(this));
			oc.Close();
		}

		private sealed class _ICodeBlock_21 : ICodeBlock
		{
			public _ICodeBlock_21(PreventMultipleOpenTestCase _enclosing)
			{
				this._enclosing = _enclosing;
			}

			/// <exception cref="System.Exception"></exception>
			public void Run()
			{
				this._enclosing.OpenFile();
			}

			private readonly PreventMultipleOpenTestCase _enclosing;
		}

		public virtual void TestReopen()
		{
			IObjectContainer oc = OpenFile();
			oc.Close();
			oc = OpenFile();
			Assert.IsNotNull(oc);
			oc.Close();
		}

		private IObjectContainer OpenFile()
		{
			return QdbEmbedded.OpenFile(NewConfiguration(), TempFile());
		}
	}
}
